'''
Created on Sept 16, 2013

@author: Patrick Janssen
'''
import sys, os
from core.modules.vistrails_module import Module, ModuleError, NotCacheable
from constants import *

''' ---------------------------------------------------------------------------
FilePrint
--------------------------------------------------------------------------- '''
class FilePrint(NotCacheable, Module):
    def compute(self):

        #Check inputs
        if not self.hasInputFromPort("input_file"):
            raise ModuleError(self, "Needs input_file")
        input_file = self.getInputFromPort("input_file")

        #Get the paths of the files
        input_file_path = os.path.abspath(input_file.name)

        #Print the file
        f = open(input_file_path)
        for line in f:
            print line
        f.close()

    _input_ports = [
        ("input_file",FILE,{"optional":False,"sort_key":1}) 
    ]

''' ---------------------------------------------------------------------------
FileView
--------------------------------------------------------------------------- '''
class FileView(NotCacheable, Module):
    def compute(self):

        #Check inputs
        if not self.hasInputFromPort("input_file"):
            raise ModuleError(self, "Needs input_file")
        if not self.hasInputFromPort("viewer"):
            raise ModuleError(self, "Needs viewer")
        input_file = self.getInputFromPort("input_file")
        viewer = self.getInputFromPort("viewer")

        #Get the paths of the files
        input_file_path = os.path.abspath(input_file.name)

        #Print the file
        command = '"' + viewer.name + '" ' + input_file_path
        os.system(command)

    _input_ports = [
        ("input_file",FILE,{"optional":False,"sort_key":1}), 
        ("viewer",FILE,{"optional":False,"sort_key":1,"defaults":str(["notepad.exe"])})
    ]

''' ---------------------------------------------------------------------------
FileViewPlus - Uses Notepad++ (hardcoded)
--------------------------------------------------------------------------- '''
class FileViewPlus(NotCacheable, Module):
    def compute(self):

        #Check inputs
        if not self.hasInputFromPort("input_file"):
            raise ModuleError(self, "Needs input_file")
        input_file = self.getInputFromPort("input_file")

        #Get the paths of the files
        input_file_path = os.path.abspath(input_file.name)

        #View the file using Notepad++
        #This assumes that notepad++ is on the path
        command = 'notepad++ ' + input_file_path
        os.system(command)

    _input_ports = [
        ("input_file",FILE,{"optional":False,"sort_key":1}), 

    ]